import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

// 使用组合式 API 定义 store
export const useCounterStore = defineStore(
  'counter',
  () => {
    // 状态
    const count = ref(0)
    const name = ref('计数器')

    // getters
    const doubleCount = computed(() => count.value * 2)
    
    // 带参数的 getter 函数
    const doubleCountPlus = (num: number) => count.value * 2 + num

    // actions
    function increment() {
      count.value++
    }

    function decrement() {
      count.value--
    }

    function reset() {
      count.value = 0
    }

    function incrementBy(value: number) {
      count.value += value
    }

    return {
      count,
      name,
      doubleCount,
      doubleCountPlus,
      increment,
      decrement,
      reset,
      incrementBy
    }
  },
  {
    // 持久化配置
    persist: true // 默认使用 localStorage 存储全部状态
  }
) 